package server;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * @Authther: mrwu
 * @date:2021/3/29-03
 * @Description:server
 */
//每个context中必须包含一个web.xml文件
public class Context {
    //存放的路径
   private String docBase;
   //表示访问的uri前缀路径
   private String path;
   private List<Wrapper> wrappers=new ArrayList<>();
   private HashMap<String,HttpServlet> servletMappings=  new HashMap<>();
   private ClassLoader classLoader;

    public String getDocBase() {
        return docBase;
    }

    public void setDocBase(String docBase) {
        this.docBase = docBase;
    }

    public Context(String docBase, String path) {
        this.docBase = docBase;
        this.path = path;
        //设置当前应用的类加载器
        Myclassloader myclassloader = new Myclassloader(docBase,"" );
        classLoader=myclassloader;
        //解析当前包下的web.xml
        loadconfig();
    }

    private void loadconfig() {
        File file = new File(docBase + "/web.xml");

        SAXReader saxReader = new SAXReader();

        try {
            FileInputStream inputStream = new FileInputStream(file);
            Document document = saxReader.read(inputStream);
            Element rootElement = document.getRootElement();

            List<Element> selectNodes = rootElement.selectNodes("//servlet");
            for (int i = 0; i < selectNodes.size(); i++) {
                Element element =  selectNodes.get(i);
                // <servlet-name>lagou</servlet-name>
                Element servletnameElement = (Element) element.selectSingleNode("servlet-name");
                String servletName = servletnameElement.getStringValue();
                // <servlet-class>server.LagouServlet</servlet-class>
                Element servletclassElement = (Element) element.selectSingleNode("servlet-class");
                String servletClass = servletclassElement.getStringValue();


                // 根据servlet-name的值找到url-pattern
                Element servletMapping = (Element) rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
                // /lagou
                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();
                Wrapper wrapper = new Wrapper();
                wrapper.setName(servletName);
                wrapper.setServletClassname(servletClass);
                wrapper.setUrlpattern(path+urlPattern);//servlet的真实路径

                HttpServlet instance =(HttpServlet)classLoader.loadClass(servletClass).newInstance();
                wrapper.setInstance(instance);
                wrappers.add(wrapper);
                servletMappings.put(wrapper.getUrlpattern(),wrapper.getInstance());
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Map<String, HttpServlet> getServletMappings() {
        return servletMappings;
    }

    public void setServletMappings(HashMap<String, HttpServlet> servletMappings) {
        this.servletMappings = servletMappings;
    }
}
